/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.icanci.loopstack.pcc.model;

import java.util.function.Function;

/**
 * 每一个元组的执行单元构件
 * 
 * @author icanci
 * @since 1.0 Created in 2024/08/02 14:53
 */
public abstract class MetaCell<Req, Resp, Target> {
    private final Req                 req;

    private Target                    target;

    private final Function<Req, Resp> function;

    public MetaCell(Req req, Function<Req, Resp> function) {
        this.req = req;
        this.function = function;
    }

    /**
     * 返回元组唯一Key，其在每个组中是唯一的
     * 
     * @return 元组唯一Key
     */
    public abstract String uniqueKey();

    /**
     * 构建执行单元
     */
    public void adapter() {
        Resp resp = function.apply(req);
        target = adapter(resp);
    }

    public abstract Target adapter(Resp resp);

    public Target getTarget() {
        // 此处进行FutureTask#get() 等待并行执行结果
        return target;
    }
}
